package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.utils.WorkDayUtils;
import com.cookor.poi.common.utils.excel.ExportExcel;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.dto.ScheduleExportDto;
import com.cookor.poi.modules.phy.entity.TSchedue;
import com.cookor.poi.modules.phy.service.TSchedueService;
import com.cookor.poi.modules.phy.service.TSchedueTemplateService;
import com.cookor.poi.modules.sys.utils.DictUtils;
import com.cookor.poi.modules.sys.utils.UserUtils;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tSchedue"})
public class TSchedueController extends BaseController {
  @Autowired
  private TSchedueService tSchedueService;
  
  @ModelAttribute
  public TSchedue get(@RequestParam(required = false) String id) {
    TSchedue entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tSchedueService.get(id); 
    if (entity == null)
      entity = new TSchedue(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tSchedue:view"})
  @RequestMapping({"list", ""})
  public String list(TSchedue tSchedue, HttpServletRequest request, HttpServletResponse response, Model model) {
    tSchedue.setTenantId(UserUtils.getTenantId());
    if (tSchedue.getScheduleMonth() == null)
      tSchedue.setScheduleMonth(Integer.valueOf(DateUtils.getMonth())); 
    if (tSchedue.getScheduleYear() == null)
      tSchedue.setScheduleYear(Integer.valueOf(DateUtils.getYear())); 
    Page<TSchedue> page = this.tSchedueService.findPage(new Page(request, response), tSchedue);
    Integer month = tSchedue.getScheduleMonth();
    Integer year = tSchedue.getScheduleYear();
    Calendar c = Calendar.getInstance();
    c.set(1, year.intValue());
    c.set(2, month.intValue() - 1);
    c.set(5, 1);
    List<Integer> list = new ArrayList<>();
    while (c.get(2) == month.intValue() - 1) {
      list.add(Integer.valueOf(c.get(7)));
      c.add(5, 1);
    } 
    model.addAttribute("days", list);
    model.addAttribute("tSchedue", tSchedue);
    model.addAttribute("page", page);
    return "modules/phy/tSchedueList";
  }
  
  @RequiresPermissions({"phy:tSchedue:view"})
  @RequestMapping({"form"})
  public String form(TSchedue tSchedue, Model model) {
    model.addAttribute("month", tSchedue.getScheduleMonth());
    model.addAttribute("admin", UserUtils.getUser());
    model.addAttribute("tSchedue", tSchedue);
    return "modules/phy/tSchedueForm";
  }
  
  @RequiresPermissions({"phy:tSchedue:edit"})
  @RequestMapping({"save"})
  public String save(TSchedue tSchedue, Model model, RedirectAttributes redirectAttributes) throws Exception {
    if (!beanValidator(model, tSchedue, new Class[0]))
      return form(tSchedue, model); 
    this.tSchedueService.mySave(tSchedue);
    addMessage(redirectAttributes, new String[] { "保存排班成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tSchedue/?repage";
  }
  
  @RequiresPermissions({"phy:tSchedue:edit"})
  @RequestMapping({"delete"})
  public String delete(TSchedue tSchedue, RedirectAttributes redirectAttributes) {
    this.tSchedueService.delete(tSchedue);
    addMessage(redirectAttributes, new String[] { "删除排班成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tSchedue/?repage";
  }
  
  @RequiresPermissions({"phy:tSchedue:view"})
  @RequestMapping(value = {"export"}, method = {RequestMethod.POST})
  public String exportFile(TSchedue tSchedue, HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
    tSchedue.setTenantId(UserUtils.getTenantId());
    if (tSchedue.getScheduleMonth() == null)
      tSchedue.setScheduleMonth(Integer.valueOf(DateUtils.getMonth())); 
    if (tSchedue.getScheduleYear() == null)
      tSchedue.setScheduleYear(Integer.valueOf(DateUtils.getYear())); 
    Page<TSchedue> page = this.tSchedueService.findPage(new Page(request, response), tSchedue);
    Integer month = tSchedue.getScheduleMonth();
    Integer year = tSchedue.getScheduleYear();
    Calendar c = Calendar.getInstance();
    c.set(1, year.intValue());
    c.set(2, month.intValue() - 1);
    c.set(5, 1);
    ArrayList<String> header = new ArrayList<>();
    header.add("所属数据");
    header.add("所属诊室");
    header.add("医生姓名");
    header.add("年份");
    while (c.get(2) == month.intValue() - 1) {
      header.add(tSchedue.getScheduleMonth() + "月" + c.get(5) + "日(" + WorkDayUtils.getChineseWeek(c) + ")");
      c.add(5, 1);
    } 
    header.add("备注");
    String fileName = "排班列表数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
    ExportExcel excel = new ExportExcel("排班列表", header);
    ScheduleExportDto dto = new ScheduleExportDto();
    label39: for (TSchedue bean : page.getList()) {
      int i = 0;
      Row cells = excel.addRow();
      excel.addCell(cells, i, bean.getTenant().getTenantName(), 2, String.class);
      i++;
      excel.addCell(cells, i, bean.getOffice().getOfficeName(), 2, String.class);
      i++;
      excel.addCell(cells, i, bean.getDoctor().getDoctorName(), 2, String.class);
      i++;
      excel.addCell(cells, i, bean.getScheduleYear(), 2, String.class);
      i++;
      while (true) {
        dto = TSchedueTemplateService.getSchedueForExport(dto, Integer.valueOf(i - 3), bean);
        if (dto != null) {
          if (dto.getWeekStatus() == null)
            continue label39; 
          if (dto.getWeekStatus().intValue() == 1) {
            excel.addCell(cells, i, DictUtils.getDictLabel(dto.getWeekStatus().toString(), "schedue_template", "") + "(号源数：" + dto
                .getAmStock() + ")", 2, String.class);
          } else if (dto.getWeekStatus().intValue() == 2) {
            excel.addCell(cells, i, DictUtils.getDictLabel(dto.getWeekStatus().toString(), "schedue_template", "") + "(号源数：" + dto
                .getPmStock() + ")", 2, String.class);
          } else if (dto.getWeekStatus().intValue() == 3) {
            excel.addCell(cells, i, DictUtils.getDictLabel(dto.getWeekStatus().toString(), "schedue_template", "") + "(号源数：" + (dto
                .getAmStock().intValue() + dto.getPmStock().intValue()) + ")", 2, String.class);
          } else if (dto.getWeekStatus().intValue() == 4) {
            excel.addCell(cells, i, DictUtils.getDictLabel(dto.getWeekStatus().toString(), "schedue_template", ""), 2, String.class);
          } 
          i++;
          continue;
        } 
        continue label39;
      } 
    } 
    excel.write(response, fileName).dispose();
    return "redirect:" + Global.getAdminPath() + "/phy/tSchedue/?repage";
  }
}
